using System;
using System.Collections.Generic;
using System.Text;
using Decav.Data;
using Decav.Gatsb.Application;

namespace Decav.Gatsb.Data
{
    /// <summary>
    /// Creates a new object that lazy loads a reference of a <see cref="User"/> from the database
    /// or other data store.
    /// </summary>
    [Serializable]
    public class UserLazyLoadReference : LazyLoadReference<Guid, User>
    {
        /// <summary>
        /// Initializes a new <see cref="UserLazyLoadReference"/>.
        /// </summary>
        /// <param name="userId">The ID of the User, or <see cref="System.Guid.Empty"/>
        /// to specify no user.</param>
        public UserLazyLoadReference(Guid userId)
            : base(userId)
        {
        }

        public UserLazyLoadReference(User user)
            : base(user.Id, user)
        {
        }

        protected override User LoadValue()
        {
            if (Id == Guid.Empty)
                return null;

            if (GatsbApplication.Current.UserPresent && GatsbApplication.Current.CurrentUser.Id == Id)
                return GatsbApplication.Current.CurrentUser;

            return GatsbApplication.Current.GetUser(Id);
        }
    }
}
